<template>
	<view class="box">
		<view class="row">
			<text>手机号</text>
			<input type="text" v-model="phone" maxlength="12"/>
		</view>
		<view class="row">
			<view class="send">
				<button type="primary" :disabled="!codeTag" size="mini" class="captcha" @click="getCode">验证码</button>
				<text>{{count}}</text>
			</view>
			<input type="text" v-model="code"  placeholder="- - - -" maxlength="4" />
		</view>
		<view class="row">
			<text style="font-size: 23rpx;">收不到验证码？试试 <text style="color:#00BB00;font-size: 23rpx;"> 语音验证</text></text>
			<!--   -->
			<button type="primary" :disabled="!loginTag" style="width: 90%;border-radius: 80rpx;margin-top: 50rpx;" @click="login">登录</button>
		</view>
		<view class="row">
			<text style="color: #006699;text-align: center;" >通过微信授权登录</text>
		</view>
	</view>
</template>
<script>
	export default{
		data(){
			return{
				phone:"",			//手机号
				code:"",			//验证码
				codeTag:false,      //false 验证码不可点击，true可以点击
				loginTag:false,     //false 登录按钮不可点击，true可以点击
				count:""            //倒计时
			}
			
		},
		//侦听
		watch:{
			phone:function(val){
				//检测手机号是否合法
				let result=/^1[3456789]\d{9}$/.test(val)
				this.codeTag=result;
			},
			code:function(val){
				if(val.length==4){
					this.loginTag=true
				}else{
					this.loginTag=false
				}
			}
	},
		methods:{
			
			//点击登录
			async login(){
				let codeStorage=uni.getStorageSync("code");
				if(this.code==codeStorage){
					//执行登录
					let result =await this.$http({url:"api/wxlogin",data:{phone:this.phone}}).catch(err=>{
						console.error(err)
					})
					// console.log(result)
					uni.setStorageSync("userInfo",result.data.list)
					//跳转mine页面
					uni.switchTab({
						url:"../mine/mine"
					})
				}else{
					alert('验证码错误！请重新获取！')
				}
				
			},
			//获取验证码
			async getCode(){
				//验证码按钮默认不可用
				this.codeTag=false;
			    //倒计时算秒
				let count=60
				 count--
				 this.count=count;
				 let timer=setInterval(()=>{
					 if(count<=0){
						 clearInterval(timer)
						 this.codeTag=true;
						 this.count=""
						 return;
					 }
					 count--
					 this.count=count;
				 },1000)
			
				let result =await this.$http({url:"api/sms",data:{phone:this.phone}}).catch(err=>{
					console.error(err)
				})
				// console.log(result)
				uni.setStorageSync("code",result.data.list.code)
			}
		    
		}
	}
</script>
<style>
	/* 导入外部的样式文件 */
	/* @import url("../../common/css/index.css"); */
	.box {
		position: relative;
	}

	.row {
		display: flex;
		flex-direction: column;
		justify-content: space-around;
		margin: 40rpx;
	}

	.send {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.send text:nth-of-type(2) {
		color: #00BB00;
	}

	text {
		font-size: 27rpx;
		margin-bottom: 10rpx;
	}

	input {
		border-bottom: 1rpx solid gray;
		height: 65rpx;
	}

	.captcha {
		background: #007cfe;
		color: white;
		padding: 10rpx 20rpx;
		border-radius: 13rpx;
		margin:0rpx;
	}
</style>
